home *** CD-ROM | disk | FTP | other *** search
-
- -- Integer math class.
- -- Can't be created, inherit from it!
-
- indexing
- names: math;
- author: "Guichard Damien";
- created: 10,November,1995;
- modified: 10,November,1995
-
- class MATH creation -- empty creation clause, so can't be created.
- feature {NONE}
- -- Facilities for heirs only.
- abs(n:INTEGER):INTEGER is
- -- Absolute value of n.
- do
- if n >= 0 then Result := n else Result := -n end
- end; -- abs
- max(a,b:INTEGER):INTEGER is
- -- Maximum value of a and b.
- do
- if a >= b then Result := a else Result := b end
- end; -- max
- min(a,b:INTEGER):INTEGER is
- -- Minimum value of a and b.
- do
- if a <= b then Result := a else Result := b end
- end; -- min
- even(n:INTEGER):BOOLEAN is
- -- Is 'n' even?
- do
- Result := n \\ 2 = 0
- ensure -- Result = n \\ 2 = 0
- end; -- even
- odd(n:INTEGER):BOOLEAN is
- -- Is 'n' odd?
- do
- Result := n \\ 2 = 1
- ensure -- Result = n \\ 2 = 1
- end; -- odd
- gcd(a,b:INTEGER):INTEGER is
- -- Greater common divider of a and b.
- require -- a >= 0; b >= 0
- local c:INTEGER
- do
- from
- Result := a;
- c := b
- until Result = c
- loop
- if Result > c then
- Result := Result - c
- else
- c := c - Result
- end
- end
- end; -- pgcd
- power(n,p:INTEGER):INTEGER is
- -- n ^ p.
- require -- p >= 0
- local i:INTEGER
- do
- if p = 0 then
- Result := 0
- else
- from
- i := 1;
- Result := n
- until i = p
- loop
- i := i + 1;
- Result := Result * n
- end
- end
- ensure -- p = 0 implies Result = 0
- end; -- power
- faculty(n:INTEGER):INTEGER is
- -- n!
- require -- n >= 0
- do
- if n = 0 then
- Result := 1
- else
- Result := n * faculty(n-1)
- end
- end -- faculty
- end -- class 'MATH'
-
-